package com.guigu.serviceorder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.common_util.pojo.utils.Result;
import com.guigu.serviceorder.entity.TbOrderItem;
import com.guigu.serviceorder.mapper.TbOrderItemMapper;
import com.guigu.serviceorder.service.ITbOrderItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2020-07-08
 */
@Service
public class TbOrderItemServiceImpl extends ServiceImpl<TbOrderItemMapper, TbOrderItem> implements ITbOrderItemService {
    @Override
    public Result findPage(Map<String, Object> searchMap, Integer page, Integer size) {
        QueryWrapper<TbOrderItem> wrapper = createExample(searchMap);
        //创建page对象
        Page<TbOrderItem> pageTbOrderItem= new Page<>(page,size);
        baseMapper.selectPage(pageTbOrderItem,null);
        if (pageTbOrderItem.getRecords() == null && pageTbOrderItem.getTotal() == 0){
            return Result.error();
        }
        return Result.ok().data("rows",pageTbOrderItem.getRecords()).data("total",pageTbOrderItem.getTotal());
    }

    // 根据查询条件封装模块对象
    private QueryWrapper<TbOrderItem> createExample(Map<String, Object> searchMap) {
        QueryWrapper<TbOrderItem> wrapper = new QueryWrapper<>();
        if(searchMap!=null) {
            if(searchMap.get("id")!=null)
                wrapper.eq("id",searchMap.get("id"));
        }
        return wrapper;
    }
}
